using System;
using System.Collections.Generic;
using System.Text;
using ServerPattern;
using System.IO;
using System.Net.Sockets;
using SyncedFileAccess;

namespace HttpServer
{
    /// <summary>
    /// TODO: TEST
    /// </summary>
	public class HttpHandler : AbstractHandler
	{
		private StreamReader sr;
		private StreamWriter sw;
        private NetworkStream nws;
        private SyncedFile sf; //output to the device
        
		public HttpHandler(Socket client, SyncedFile f) : base (client) {
            nws = new NetworkStream(client,true);
			sr = new StreamReader(nws);
			sw = new StreamWriter(nws);
            sf = f;
		}

		override protected Boolean ReadRequest()
		{
			String request = sr.ReadLine();
			Console.WriteLine(request);
			return request.Contains("GET");
		}

		override protected void CreateResponse()
		{
			//String theData = new StreamReader("protokoll.txt").ReadToEnd();
            String theData = sf.ReadFile();
            sw.WriteLine("HTTP/0.5 200 OK");
			sw.WriteLine("Content-type: text/plain");
			sw.WriteLine("Content-length: " + theData.Length);
			sw.WriteLine();
			sw.Write(theData);
			sw.Flush();
			Console.WriteLine("File gesendet");
		}

        
	}
}
